package hn.cch.log.logback;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.classic.spi.Configurator;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.util.StatusPrinter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URL;

/**
* @author CCH
* @version 2019.01.04
*/
public class TestLogback {


   private static Logger logger = LoggerFactory.getLogger(TestLogback.class);

   public static void main(String[] args) {

       //加载XML配置文件
       //System.setProperty(ContextInitializer.CONFIG_FILE_PROPERTY, "/logback/logback.xml");

       LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
       JoranConfigurator joranConfigurator = new JoranConfigurator();
       joranConfigurator.setContext(loggerContext);
       loggerContext.reset();

       try {
           //joranConfigurator.doConfigure("C:\\Users\\chench\\Desktop\\IntelliJ IDEA\\log\\src\\main\\resource\\logback\\logback.xml");

           URL url = Configurator.class.getClassLoader().getResource("logback/logback.xml");
           joranConfigurator.doConfigure(url);

       } catch (JoranException e) {
           e.printStackTrace();
       }
       StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);

       for (int i = 0; i < 10; i++) {
           try {
               Thread.sleep(1000);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
           logger.error("error" + i);
       }


   }
}
